from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果
        self.camera.frame.save_state()
        self.play(FadeIn(dot1),FadeIn(dot2),FadeIn(dot3))
         #中心主题+框框标记
        text1 = Tex(r"\text{分部积分}",color=PINK
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-37,15,0])
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=23,height=8
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-37, 15, 0])

        self.play(Create(rectangle),Create(text1))

        #箭头引出
        arrow = Arrow(start=[-25,18,0],end=[-17,25,0], color=RED
                       , stroke_width=20, tip_length=1)
        #箭头引出
        arrow1 = Arrow(start=[-25,16,0],end=[-17,17,0], color=RED
                       , stroke_width=20, tip_length=1)
        #箭头引出
        arrow2 = Arrow(start=[-25,14,0],end=[-17,9,0], color=RED
                       , stroke_width=20, tip_length=1)
         #箭头引出
        arrow3 = Arrow(start=[-25,12,0],end=[-17,1,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow),Create(arrow1),Create(arrow2),Create(arrow3))

        #文本
        text2 = Tex(r"\text{分母降幂}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,25,0])
        text4 = Tex(r"$\int \dfrac{xe^{x}}{\left( 1+x\right) ^{2}}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([23,25,0])
        
        text3 = Tex(r"\text{积分抵消}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,17,0])
        text5 = Tex(r"$\int \dfrac{1+\sin x}{1+\cos x}e^{x}dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([23,17,0])
        
        text6 = Tex(r"\text{构造递推}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,9,0])
        text7 = Tex(r"$\int ^{1}_{0}x^{2}\ln ^{n}xdx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([23,9,0])
        text8 = Tex(r"\text{简化计算}",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,1,0])
        text9 = Tex(r"\text{设}$f\left( x\right) =\int _{0}^{x}\dfrac{\sin t}{\pi -t}dt$\text{,计算}$\int _{0}^{\pi }f\left( x\right) dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([23,1,0])
        self.play(Create(text2),Create(text3),Create(text6),Create(text8))
        self.play(Write(text4),Write(text5),Write(text7),Write(text9))

        #框框标记
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=15,height=8
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle.move_to([23,25,0])
        self.play(Create(rectangle))
        
        #例题1讲解
        text10 = Tex(r"\text{解：}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).move_to([-40,-5,0])
        text11 = Tex(r"$\int \dfrac{xe^{x}}{\left( 1+x\right) ^{2}}dx=$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text12 = Tex(r"$\int xe^{x}d\dfrac{-1}{x+1}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([3,-10,0])
        text13 = Tex(r"$\dfrac{-xe^{x}}{x+1}+\int \dfrac{1}{x+1}dxe^{x}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([3,-10,0])
        text14 = Tex(r"$\dfrac{-xe^{x}}{x+1}+\int e^{x}dx$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([3,-10,0])
        text15 = Tex(r"$\dfrac{-xe^{x}}{x+1}+e^{x}+c$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([3,-10,0])
        text16 = Tex(r"$\dfrac{e^{x}}{x+1}+c$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([3,-10,0])
        self.play(Write(text10))

        self.play(Write(text11))
        #镜头拉近
        self.play(self.camera.frame.animate.scale(0.8).move_to([0,-10,0]))

        #箭头引出
        arrow = Arrow(start=[7,-17,0],end=[4,-14,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本
        long_text=("特征:\n"
                     "分母整体平方")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([17,-17,0])
        
        self.play(Create(text100))
        self.play(FadeOut(text100),FadeOut(arrow),Restore(self.camera.frame))
        self.play(text11.animate.scale(1).move_to([-20,-10,0]))
        self.play(Write(text12))
        
        self.play(
            ReplacementTransform(text12,text13)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text13,text14)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text14,text15)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text15,text16)
        )
        self.wait(0.5)

        #例题1清空
        self.play(FadeOut(text16),FadeOut(text11))
        #框框移动
        self.play(rectangle.animate.stretch(1.3,dim=0).move_to([23,17,0]))

        #例题2讲解
        
        
        text11 = Tex(r"$\int \dfrac{1+\sin x}{1+\cos x}e^{x}dx=$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text12 = Tex(r"$\int \dfrac{1}{1+\cos x}e^{x}dx+\int \dfrac{\sin x}{1+\cos x}e^{x}dx$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        
        text13 = Tex(r"$\int \dfrac{\sin x}{1+\cos x}de^{x}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-18,0])
        text14 = Tex(r"$\dfrac{\sin x}{1+\cos x}e^{x}-\int e^{x}d\dfrac{\sin x}{1+\cos x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-18,0])
        text15 = Tex(r"$\dfrac{e^{x}\sin x}{1+\cos x}-\int \dfrac{1}{1+\cos x}e^{x}dx$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-18,0])
        text16 = Tex(r"$\int \dfrac{1+\sin x}{1+\cos x}e^{x}dx=\dfrac{\sin x}{1+\cos x}e^{x}+c$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-10,0])
       
        self.play(Write(text11))
        #镜头拉近
        self.play(self.camera.frame.animate.scale(0.8).move_to([0,-6,0]))

        #箭头引出
        arrow = Arrow(start=[7,-5,0],end=[2,-6,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本
        long_text=("特征:\n"
                     "分子拆分暗示")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([17,-5,0])
        
        self.play(Create(text100))
        self.play(FadeOut(text100),FadeOut(arrow),Restore(self.camera.frame))
        
        self.play(
            ReplacementTransform(text11,text12)
        )
        self.wait(0.5)
        text17 = Tex(r"$\left( \dfrac{\sin x}{1+\cos x}\right) ^{'}=\dfrac{1}{1+\cos x}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([3,-18,0])
        self.play(Write(text17))
        self.play(FadeOut(text17))
        #下划线
        dot1=Dot([2,-14,0])
        dot2=Dot([19,-14,0])
        line=Line(dot1,dot2).set_color(PURPLE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        self.play(
            Write(text13)
        )
        self.wait(0.5)
        self.play(
            ReplacementTransform(text13,text14)
        )
        self.wait(0.5)
        #下划线
        dot3=Dot([10,-22,0])
        dot4=Dot([25,-22,0])
        line1=Line(dot3,dot4).set_color(PURPLE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line1))
        self.play(FadeOut(line1))
        self.play(
            ReplacementTransform(text14,text15)
        )
        #框框标记
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=18,height=8
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle1.move_to([-11,-10,0])
        #框框标记
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=18,height=8
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle2.move_to([17.3,-18,0])
        self.play(Create(rectangle2),Create(rectangle1))
        self.wait(0.5)

        self.play(
            ReplacementTransform(text15,text16),
            ReplacementTransform(rectangle2,text16),
            ReplacementTransform(rectangle1,text16),
            ReplacementTransform(line,text16),
            ReplacementTransform(text12,text16),
        )
        self.wait(0.5)

        #例题1清空
        self.play(FadeOut(text16))
        #框框移动
        self.play(rectangle.animate.stretch(1,dim=0).move_to([23,9,0]))
        
       #例题3讲解
        
        
        text11 = Tex(r"$\int _{0}^{1}x^{2}\ln ^{n}xdx=$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text12 = Tex(r"$\int _{0}^{1}\ln ^{n}xd\dfrac{x^{3}}{3}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        
        text13 = Tex(r"$ \left( \dfrac{x^{3}}{3}\ln ^{n}x\right) | _{0}^{1}-\dfrac{n}{3}\int _{0}^{1}x^{2}\ln ^{n-1}xdx$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-10,0])
        text14 = Tex(r"$-\dfrac{n}{3}\int _{0}^{1}x^{2}\ln ^{n-1}xdx$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-10,0])
        text140 = Tex(r"$\text{令}I_{n}=\int ^{1}_{0}x^{2}\ln ^{n}xdx$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-18,0])
        text15 = Tex(r"$I_{n}=-\dfrac{n}{3}I_{n-1}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text16 = Tex(r"$=\ldots =\dfrac{\left( -1\right) ^{n}\cdot n!}{3^{n}}I_{0}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text17 = Tex(r"$I_{0}=\int _{0}^{1}x^{2}dx=\dfrac{1}{3}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,-18,0])
        text18 = Tex(r"$=\dfrac{\left( -1\right) ^{n}\cdot n!}{3^{n+1}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-10,0])
        self.play(Write(text11))
        #镜头拉近
        self.play(self.camera.frame.animate.scale(0.8).move_to([0,-6,0]))

        #箭头引出
        arrow = Arrow(start=[7,-5,0],end=[2,-6,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本
        long_text=("特征:\n"
                     "出现字母n")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([17,-5,0])
        
        self.play(Create(text100))
        self.play(FadeOut(text100),FadeOut(arrow),Restore(self.camera.frame))
        self.play(text11.animate.scale(1).move_to([-20,-10,0]))
        self.play(Write(text12))




        self.play(
            ReplacementTransform(text12,text13)
        )
        self.wait(0.5)
        
        
        
        self.play(
            ReplacementTransform(text13,text14)
        )
        self.wait(0.5)
        self.play(Write(text140))
        self.play(FadeOut(text140))
        self.play(
            ReplacementTransform(text14,text15),
            ReplacementTransform(text11,text15)
        )
       
        self.wait(0.5)
        self.play(text15.animate.scale(1).move_to([-20,-10,0]))

        self.play(
            Write(text16)
           
        )
        self.wait(0.5)
        self.play(
            Write(text17)
           
        )
        self.play(
            FadeOut(text17)
           
        )
        self.play(
            Write(text18)
           
        )
        #例题1清空
        self.play(FadeOut(text18),FadeOut(text16),FadeOut(text15))
        #框框移动
        self.play(rectangle.animate.stretch(2.2,dim=0).move_to([23,1,0]))
        

        #例题3讲解
        
        
        text11 = Tex(r"$\int _{0}^{\pi }f\left( x\right) dx$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])

        self.play(
            Write(text11)
           
        )
        #框框标记
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=8
                            ,stroke_color=PURE_RED,stroke_width=20)
        rectangle1.move_to([15,1,0])
        self.play(Create(rectangle1))
        self.play(FadeOut(rectangle1))
        text12 = Tex(r"$ xf\left( x\right) | _{0}^{\pi }-\int _{0}^{\pi }xdf\left( x\right) $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])

        self.play(
            ReplacementTransform(text11,text12)
           
        )
        #下划线
        dot3=Dot([1,-12,0])
        dot4=Dot([12,-12,0])
        line1=Line(dot3,dot4).set_color(PURPLE)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line1))
        self.play(FadeOut(line1))
        text13 = Tex(r"$ \pi f\left( \pi \right) -\int _{0}^{\pi }xf^{'}\left( x\right) dx$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])

        self.play(
            ReplacementTransform(text12,text13)
           
        )
        text14 = Tex(r"$\pi f\left( \pi \right) -\int _{0}^{\pi }x\dfrac{\sin x}{\pi -x}dx$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.wait(0.5)
        self.play(
            ReplacementTransform(text13,text14)
           
        )
        text15 = Tex(r"$?$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).move_to([-8,-18,0])
        text16 = Tex(r"$?$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).move_to([5,-18,0])
        
        self.play(
            Write(text15),Write(text16)
        )
        self.play(
            FadeOut(text15),FadeOut(text16)
           
        )



        text17 = Tex(r"$\int _{0}^{\pi }f\left( x\right) d\left( x-\pi \right) $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-18,0])

        self.play(
            Write(text17)
           
        )
        text18 = Tex(r"$ \left( x-\pi \right) f\left( x\right) | _{0}^{\pi }-\int _{0}^{\pi }\left( x-\pi \right) f'\left( x\right) dx $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-18,0])

        self.play(
            ReplacementTransform(text17,text18)
           
        )
        self.wait(0.5)
        text19 = Tex(r"$0+\int _{0}^{\pi }\sin xdx$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-18,0])

        self.play(
            ReplacementTransform(text18,text19)
           
        )
        self.wait(0.5)
        text20 = Tex(r"$=2$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).move_to([0,-18,0])

        self.play(
            ReplacementTransform(text19,text20)
           
        )





        self.wait()
